# -*- coding: utf-8 -*-
import scrapy
from jdong.items import JdongItem
from jdong.middlewares import MAX_PAGE_NUM

class JdSpider(scrapy.Spider):
    name = 'jd'
    allowed_domains = ['list.jd.com/list.html']
    start_url = 'https://list.jd.com/list.html?cat=670,671,672&page=1&sort=sort_totalsales15_desc&trans=1&JL=6_0_0#J_main'

    def start_requests(self):
        for page in range(1,MAX_PAGE_NUM + 1):
            url = self.start_url
            yield scrapy.Request(url=url, callback=self.parse, meta={'page': page}, dont_filter=True)
        yield scrapy.Request(url=self.start_url, callback=self.parse, meta={'page': MAX_PAGE_NUM + 1}, dont_filter=True)

    def parse(self, response):
        li_label_list = response.xpath("//li[@class='gl-item']")
        next_url = response.xpath("//a[@class='pn-next']/@href").extract_first()
        next_url = "https://list.jd.com" + next_url
        with open("jd.html", "w") as f:
            f.write(response.body.decode())


        for li in li_label_list:
            item = JdongItem()
            # print("="*30)
            image = li.xpath(".//div[@class='p-img']/a/@href").extract_first()
            # print(image)
            price = li.xpath(".//div[@class='p-price']/strong[@class='J_price']/i/text()").extract_first()
            # print(price)
            title = li.xpath(".//div[@class='p-name']/a/em/text()").extract_first().strip()
            # print(title)
            comments = li.xpath(".//div[@class='p-commit p-commit-n']/strong/a/text()").extract_first()
            # print(comment)
            store = li.xpath(".//div[@class='p-shop']//a/@title").extract_first()
            # print(store)
            info = li.xpath("./div/div[6]//text()").extract()
            # print(info)
            item = JdongItem(image=image, price=price, title=title, comments=comments, store=store, info=info)
            yield item

            


